<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 用户列表区域 -->
      <el-table :data="userList.slice((currentPage-1)*pageSize,currentPage*pageSize)"
                border stripe style="width: 100%">
        <el-table-column label="用户ID" prop="id"></el-table-column>
        <el-table-column label="用户名称" prop="username"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="用户状态" width="300">
          <template v-slot="scope">
            <el-tag v-if="scope.row.deleted" type="warning">已停用</el-tag>
            <el-tag v-else class="unLocked" type="success">正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button v-if="!scope.row.deleted" type="danger" @click="disable(scope.row.id)">
              停用账号
            </el-button>
            <el-button v-else type="success" @click="activate(scope.row.id)">
              重新激活
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination :total="total" background
                     layout="prev, pager, next"
                     @current-change="changeUserTablePage">
      </el-pagination>

    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userList: [],
      total: 0,
      currentPage: 1,
      pageSize: 10
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      const res = await this.$http.get(`admin/allUsers`)
      if (res.code !== 200) {
        return this.$message.error('获取用户列表失败')
      }
      this.userList = res.data
      this.total = res.data.length
    },
    /**
     * @abstract 按账号编号停用某账号
     * @param {userId} id 需要停用的账号编号
     */
    async disable(id) {
      const confirmresult = await this.$confirm('此操作将停用该账号, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      //如果用户确认删除，则返回字符串confirm  错误用catch捕获，返回字符串cancel
      if (confirmresult !== 'confirm') return this.$message.info('已取消停用操作')

      const res = await this.$http.post(`admin/disable?userId=${id}`)

      if (res.code !== 200) {
        return this.$message.error(res.message)
      }
      this.$message.success(res.message)
      this.getUserList()
    },
    async activate(id) {
      const confirmresult = await this.$confirm('此操作将重新启用该账号, 是否继续?', '提示', {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "success"
      }).catch(err => err)

      if (confirmresult !== 'confirm') {
        return this.$message.info('已取消激活操作');
      }

      const res = await this.$http.post(`admin/activate?userId=${id}`);

      if (res.code !== 200) {
        return this.$message.error(res.message);
      }
      this.$message.success(res.message)
      await this.getUserList()
    },
    changeUserTablePage(currentPage) {
      this.currentPage = currentPage;
    }
  }
}
</script>